﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace ExchangeCurrencyRate.Parser
{
    public static class BloombergParser
    {
        public static Dictionary<string, double> Parse()
        {
            //http://www.bloomberg.com/js/calculators/currdata.js
            //http://www.bloomberg.com/js/calculators/currencycalc_10.js
            Dictionary<string, double> result = new Dictionary<string, double>();
            ParseWebAddress("http://www.bloomberg.com/js/calculators/currdata.js", (content) =>
                {
                    Regex regex = new Regex(@"price\[\'(?<code>[A-Z]{3}):CUR\'\]\s*=\s*(?<rate>[\d\.,]+);", RegexOptions.Multiline | RegexOptions.IgnoreCase);
                    MatchCollection matches = regex.Matches(content);
                    foreach (Match match in matches)
                    {
                        double d;
                        if (match.Success &&
                            match.Groups["code"].Success &&
                            match.Groups["rate"].Success &&
                            double.TryParse(match.Groups["rate"].Value, out d))
                        {
                            if (!result.ContainsKey(match.Groups["code"].Value))
                            {
                                result.Add(match.Groups["code"].Value, d);
                            }
                            else
                            {
                                Console.WriteLine("Duplicate: '{0}'={1};", match.Groups["code"].Value, d);
                            }
                        }
                    }
                });
            ParseWebAddress("http://www.bloomberg.com/js/calculators/currencycalc_10.js", (content) =>
            {
                Regex regex = new Regex(@"eurovals\[\'(?<code>[A-Z]{3})\'\]\s*=\s*(?<rate>[\d\.,]+);", RegexOptions.Multiline | RegexOptions.IgnoreCase);
                MatchCollection matches = regex.Matches(content);
                foreach (Match match in matches)
                {
                    double d;
                    if (match.Success &&
                        match.Groups["code"].Success &&
                        match.Groups["rate"].Success &&
                        double.TryParse(match.Groups["rate"].Value, out d))
                    {
                        if (!result.ContainsKey(match.Groups["code"].Value))
                        {
                            result.Add(match.Groups["code"].Value, d);
                        }
                        else
                        {
                            result[match.Groups["code"].Value] = d;
                        }
                    }
                }
            });
            return result;
        }

        private static void ParseWebAddress(string webAddress, Action<string> action)
        {
            WebRequest request = HttpWebRequest.Create(webAddress);
            {
                WebResponse response = request.GetResponse();
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            action.Invoke(reader.ReadToEnd());
                        }
                    }
                    response.Close();
                }
            }
        }
    }
}
